# -*- coding: utf-8 -*-
"""Setup the CRV application"""

import logging

import transaction
from tg import config

from crv.config.environment import load_environment

__all__ = ['setup_app']

log = logging.getLogger(__name__)

from crv.tools.certstoremgr import DB_CertStoreStorage, CertStoreMgr

def setup_app(command, conf, vars):
    """Place any commands to setup crv here"""
    load_environment(conf.global_conf, conf.local_conf)
    # Load the models
    from crv import model
    print "Creating tables"
    model.metadata.create_all(checkfirst=False,bind=config['pylons.app_globals'].sa_engine)

    #setup CA
    caMgr=CertStoreMgr.getInstance()
    #if in nosetests do not create ca information
    if not config.get('cloudcrv.unittest', 'False')=="True" and not caMgr.isInitialized():
        caMgr.initializeCA()

        ouf=open("/tmp/ca.pass","w")
        ouf.write(caMgr.getCAPassword())
        ouf.close()
        
        ouf=open("/tmp/ca.crt","w")
        ouf.write(caMgr.getCACertPEM())
        ouf.close()

        ouf=open("/tmp/ca.prk","w")
        ouf.write(caMgr.getCAPrivateKeyPEM())
        ouf.close()

        ouf=open("/tmp/ca.puk","w")
        ouf.write(caMgr.getCAPublicKeyPEM())
        ouf.close()

        #create a certificate for both apache virtual host (turbogears)
        caMgr.generateCert("cloudcrvsrv")

        ouf=open("/tmp/cloudcrvsrv.crt","w")
        ouf.write(caMgr.getCertPEM("cloudcrvsrv"))
        ouf.close()

        ouf=open("/tmp/cloudcrvsrv.prk","w")
        ouf.write(caMgr.getPrivateKeyPEM("cloudcrvsrv"))
        ouf.close()

        ouf=open("/tmp/cloudcrvsrv.puk","w")
        ouf.write(caMgr.getPublicKeyPEM("cloudcrvsrv"))
        ouf.close()

        ouf=open("/tmp/cloudcrvsrv.pass","w")
        ouf.write(caMgr.getPassword("cloudcrvsrv"))
        ouf.close()
        
        caMgr.generateCert("puppetsrv")

        ouf=open("/tmp/puppetsrv.crt","w")
        ouf.write(caMgr.getCertPEM("puppetsrv"))
        ouf.close()

        ouf=open("/tmp/puppetsrv.prk","w")
        ouf.write(caMgr.getPrivateKeyPEM("puppetsrv"))
        ouf.close()

        ouf=open("/tmp/puppetsrv.puk","w")
        ouf.write(caMgr.getPublicKeyPEM("puppetsrv"))
        ouf.close()

        ouf=open("/tmp/puppetsrv.pass","w")
        ouf.write(caMgr.getPassword("puppetsrv"))
        ouf.close()
        
    from crv.model.crvmodelsampledata import createSampleData
    createSampleData(model.metadata,model.DBSession)
    
    manager = model.User()
    manager.user_name = u'admin'
    manager.display_name = u'Example admin'
    manager.email_address = u'admin@somedomain.com'
    manager.password = u'adminpass'

    model.DBSession.add(manager)

    group = model.Group()
    group.group_name = u'managers'
    group.display_name = u'Managers Group'

    group.users.append(manager)

    model.DBSession.add(group)

    permission = model.Permission()
    permission.permission_name = u'manage'
    permission.description = u'This permission give an administrative right to the bearer'
    permission.groups.append(group)

    model.DBSession.add(permission)

    model.DBSession.flush()

    transaction.commit()
    print "Successfully setup"
